/* 
 * Karen Bonnet
 */

/*
 * Select with exclusion option's text validation
 * Required paramaters
 * id = The id of the control to validate
 * exclusionText = Text which should not be selected when the control is validate
 * divErrorMessageId = id of the div to display individual error
 * errorMessage = the error message to display when error occurs
 */
function listCustomValidation(id, exclusionText, divErrorMessageId, errorMessage) {
    this.id = id; 
    this.exclusionText = exclusionText;
    this.divErrorMessageId = divErrorMessageId;
    this.errorMessage = errorMessage;
    this.okMessage='';
    this.isDisabled = false ;
    this.errorOccured = false;
    this.styleClass ='validationCustomOk';
    
    /*
     * Call this method on submit
     * Parameters boolean
     * true = single validation
     */
    this.processValidation = function() {
        var select_list_field = document.getElementById(this.id);
        var text = select_list_field.options[select_list_field.selectedIndex].text;
        var message ='';
        var styleClass;
        
        if(this.divErrorMessageId != null){
            if(this.isDisabled == false){ // The element to validate does not be disabled
                if(text == this.exclusionText){ // Verfify if the exclusion text is selected
                    message=this.errorMessage; // Prepare the error message
                    this.styleClass = 'validationCustomError'; // Assign the css class
                    this.errorOccured = true ; // To know if error occured
                }
                else{
                    message=this.okMessage; // Prepare the message to display
                    this.styleClass ='validationCustomOk'; // Assign the css class
                    this.errorOccured = false ; // To know if error occured
                }
                document.getElementById(this.divErrorMessageId).innerHTML = "<p class="+this.styleClass+">"+message+"</p>"; // Display the validation message in the corresponding div
            }
        }
    }
    
    /*
     * Call this method to know if the element passed the validation
     * Return true if something went wrong and the error message will be displayed
     */
    this.isValidationPassed = function(){
        this.processValidation();
        return this.errorOccured;
    }
    
    /* 
     * Call this method to set your own OK message
     */
    this.setOkMessage  = function(val){
        this.okMessage = val;
    };
    
    /* 
     * Call this method to enabled or disabled the validation
     */
    this.setIsDisabled  = function(val){
        this.isDisabled = val;
        var message=this.okMessage; // Prepare the message to display
        this.styleClass ='validationCustomOk'; // Assign the css class
        this.errorOccured = false ; // To know if error occured
        document.getElementById(this.divErrorMessageId).innerHTML = "<p class="+this.styleClass+">"+message+"</p>"; // Display the validation message in the corresponding div
    };
} 

/*
 * Validate textArea filled only with numbers
 * Required parameters
 * id = The id of the control to validate
 * divErrorMessageId = the id of the div used to display messages
 */

function onlyNumbersFieldCustomValidation(id, divErrorMessageId) {
    this.id = id;
    this.divErrorMessageId = divErrorMessageId;
    this.minLength = null;
    this.maxLength = null;
    this.minValue = null;
    this.maxValue = null;
    this.messageOk = '' ;
    this.lengthErrorMessage = 'You do not respect the length rule.' ;
    this.valueErrorMessage = 'You do not respect the limit value rule.';
    this.isNanErrorMessage = 'You have to enter a number!';
    this.smthgWrong = false ;
    this.styleClass ='validationCustomOk';
    this.isDisabled = false;
    this.errorOccured = false ;
    this.defaultValue = '';
    
    /* 
     * Call this method onKeyUp to validate an element on the fly to validate the entire formular
     */
    this.processValidation = function() {
        document.getElementById(id).style.color = 'black'; // set the text color of the input
        document.getElementById(id).style.fontStyle="normal";
        var valueToValidate = document.getElementById(this.id).value ; // the input value
        if(this.isDisabled == false){ // check if the element is not disabled
            if(isNaN(valueToValidate)){ // check if the value is isNan
                this.smthgWrong = true ;
                styleClass='validationCustomError'; // set the css class
                //Display the error message
                document.getElementById(this.divErrorMessageId).innerHTML = "<p class="+styleClass+">"+this.isNanErrorMessage +"</p>";
            }
            else{ // if the value is an integer
                if(this.minLength != null && this.maxLength != null){ //Check the length
                    
                    if(valueToValidate.length < this.minLength || valueToValidate.length > this.maxLength){
                        if(valueToValidate.length > 0){
                            this.smthgWrong = true ;
                            styleClass='validationCustomError';
                            document.getElementById(this.divErrorMessageId).innerHTML = "<p class="+styleClass+">"+this.lengthErrorMessage+"</p>"; // Display the error message beacause the current rule is not repected
                        }
                    }
                }
            }
        
            if(this.smthgWrong == false){ // only if nothing went wrong reset the css class and the error message
                this.styleClass ='validationCustomOk';
                document.getElementById(this.divErrorMessageId).innerHTML = "<p></p>";
                if(valueToValidate.length > 0){
                    if(this.minValue != null){
                        if(this.maxValue != null){
                            if(valueToValidate < this.minValue || valueToValidate > this.maxValue){
                                this.smthgWrong = true ;
                                styleClass='validationCustomError';
                                document.getElementById(this.divErrorMessageId).innerHTML = "<p class="+styleClass+">"+this.valueErrorMessage+"</p>"; // If the value is not between the limits display the error message
                            }
                        }
                    }
                }
                else{ // if the field is empty
                    var functionToCall = 'displayDefaultValue("'+this.id+'","'+this.defaultValue+'")';
                    var t=setTimeout(functionToCall,3000); // set timer to display the default value
                }
            }
        }
        this.errorOccured = this.smthgWrong ;
        this.smthgWrong = false ; // reset the flag
    }
    
    /* 
     * Call this function in the document ready function
     * To display the default value
     */
    displayDefaultValue = function(id, defaultValue){
        var inputValidation = document.getElementById(id);
        if(inputValidation.value.length == 0){
            inputValidation.style.fontStyle="italic";
            inputValidation.value = defaultValue;
            inputValidation.style.color = 'grey';
        }
    }
    
    /*
     * Call this method to know if the element passed the validation
     * Return true if something went wrong and the error message will be displayed
     */
    this.isValidationPassed = function(){
        this.processValidation();
        return this.errorOccured;
    }
    
    /*
     * To set the min and max length allowed
     */
    this.setLengthLimits= function(min, max){
        this.minLength = min;
        this.maxLength = max;
    }

    /*
     * To set the min and max value allowed
     */
    this.setValueLimits = function(min, max){
        this.minValue = min;
        this.maxValue = max;
    }
    
    /*
     * To set your own OK message
     */
    this.setOkMessage  = function(val){
        this.messageOk = val;
    };
   
    /*
     * To set the error message for the value limits rule
     */
    this.setErrorMessageValueLimits  = function(val){
        this.valueErrorMessage = val;
    };
    
    /*
     * To set the error message for the size limits
     */
    this.setErrorMessageLengthLimits  = function(val){
        this.lengthErrorMessage = val;
    };
   
    /*
     * To set the errror message when the value is not an integer
     */
    this.setErrorMessageIsNan  = function(val){
        this.isNanErrorMessage = val;
    };
    
    /*
     * To enebled or disabled the validation
     */
    this.setIsDisabled  = function(val){
        this.isDisabled = val;
    };

    /*
     * To set the default value to display
     */
    this.setDefaultValue  = function(val){
        this.defaultValue = val;
    };
    
    this.getDefaultValue  = function(){
        return this.defaultValue ;
    };
    
}


/*
 * Validate textArea
 * Required parameters
 * id = The id of the control to validate
 * divErrorMessageId = the id of the div used to display messages
 */

function textFieldCustomValidation(id, divErrorMessageId) {
    this.id = id;
    this.divErrorMessageId = divErrorMessageId;
    this.minLength = null;
    this.maxLength = null;
    this.messageOk = '' ;
    this.lengthErrorMessage = 'You do not respect the length rule.' ;
    this.smthgWrong = false ;
    this.styleClass ='validationCustomOk';
    this.isDisabled = false;
    this.errorOccured = false ;
    this.defaultValue = '';
    
    /* 
     * Call this method onKeyUp to validate an element on the fly to validate the entire formular
     */
    this.processValidation = function() {
        document.getElementById(id).style.color = 'black'; // set the text color of the input
        document.getElementById(id).style.fontStyle="normal";
        var valueToValidate = document.getElementById(this.id).value ; // the input value
        
        if(this.isDisabled == false){ // check if the element is not disabled
            if(this.minLength != null && this.maxLength != null){ //Check the length
                if(valueToValidate.length < this.minLength || valueToValidate.length > this.maxLength){
                    if(valueToValidate.length == 0){
                        this.smthgWrong = true ;
                        styleClass='validationCustomError';
                        document.getElementById(this.divErrorMessageId).innerHTML = "<p class="+styleClass+">"+this.lengthErrorMessage+"</p>"; // Display the error message beacause the current rule is not repected
                    }
                }
            }
            
            if(this.smthgWrong == false){ // only if nothing went wrong reset the css class and the error message
                this.styleClass ='validationCustomOk';
                document.getElementById(this.divErrorMessageId).innerHTML = "<p></p>";
                if(valueToValidate.length == 0){
                    var functionToCall = 'displayDefaultValue("'+this.id+'","'+this.defaultValue+'")';
                    var t=setTimeout(functionToCall,3000); // set timer to display the default value
                }
            }
        }
        this.errorOccured = this.smthgWrong ;
        this.smthgWrong = false ; // reset the flag
    }
    
    /* 
     * Call this function in the document ready function
     * To display the default value
     */
    displayDefaultValue = function(id, defaultValue){
        var inputValidation = document.getElementById(id);
        if(inputValidation.value.length == 0){
            inputValidation.style.fontStyle="italic";
            inputValidation.value = defaultValue;
            inputValidation.style.color = 'grey';
        }
    }
    
    /*
     * Call this method to know if the element passed the validation
     * Return true if something went wrong and the error message will be displayed
     */
    this.isValidationPassed = function(){
        this.processValidation();
        return this.errorOccured;
    }
    
    /*
     * To set the min and max length allowed
     */
    this.setLengthLimits= function(min, max){
        this.minLength = min;
        this.maxLength = max;
    }

    /*
     * To set your own OK message
     */
    this.setOkMessage  = function(val){
        this.messageOk = val;
    };
   
    /*
     * To set the error message for the size limits
     */
    this.setErrorMessageLengthLimits  = function(val){
        this.lengthErrorMessage = val;
    };
    
    /*
     * To enebled or disabled the validation
     */
    this.setIsDisabled  = function(val){
        this.isDisabled = val;
    };

    /*
     * To set the default value to display
     */
    this.setDefaultValue  = function(val){
        this.defaultValue = val;
    };
    
    this.getDefaultValue  = function(){
        return this.defaultValue ;
    };
    
}